package instant.news7;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Switch;

import androidx.appcompat.app.AppCompatDelegate;

public class ThemeDayAndNight {
    public static void ThemeChange(Activity activity, Switch swithchange) {
        //NIGHT MODE
        SharedPreferences lightDark = activity.getSharedPreferences("pre", Context.MODE_PRIVATE);
        Boolean cheak = lightDark.getBoolean("dark", false);
        swithchange.setChecked(cheak);
        if (cheak.equals(false)) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            swithchange.setChecked(false);

        } else if (cheak.equals(true)) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            swithchange.setChecked(true);
        }
        swithchange.setOnCheckedChangeListener((compoundButton, b) -> {
            SharedPreferences.Editor ed = lightDark.edit();
            if (b) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                ed.putBoolean("dark", true);
                ed.commit();

            } else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                ed.putBoolean("dark", false);
                ed.commit();

            }
        });


    }
}
